﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;
using VO.Business.Controllers;
using VO.Business.Utils;
using VO.Entities;

namespace VO.Web
{
    public partial class Tracks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //GMap1.MarkerClick +=new GMap.GMap_Handler_ServerEvent_MarkerClick(GMap1_MarkerClick);

            if (!IsPostBack)
            {
                GMap1.setCenter(new GLatLng(0.01, 0.01),1);

                GMapUIOptions options = new GMapUIOptions();
                options.maptypes_hybrid = false;
                options.keyboard = true;
                options.maptypes_physical = true;
                options.zoom_scrollwheel = true;
                options.controls_smallzoomcontrol3d = true;
                options.controls_largemapcontrol3d = false;
                options.controls_scalecontrol = false;
                GMap1.Add(new GMapUI(options));


                List<GMarker> markers = new List<GMarker>();

                TracksController.GetTracks().ForEach(delegate(Track t)
                {
                    List<LocationPoint> locationPoints = GeoUtils.DeserializeTrack(t.GeoString);
                    if (locationPoints.Count > 1)
                    {
                        LocationPoint startP = locationPoints[0];
                        GMarker marker = new GMarker(new GLatLng(startP.Latitude, startP.Longitude));
                        marker.ID = "M_" + t.Id.ToString().Replace("-", string.Empty);
                        GMarkerOptions opt =new GMarkerOptions();
                        opt.title = t.TrackName;
                        marker.options = opt;
                        markers.Add(marker);
                    }
                });
                //    //GInfoWindow window = new GInfoWindow(marker, "You can use the Labeled Marker as any other marker");
                //    //GMap1.Add(window);

                GMap1.markerClusterer = new MarkerClusterer(markers);
            }

        }

        protected string GMap1_MarkerClick(object s, GAjaxServerEventArgs e)
        {
            string markerId = e.who;
            string trackId = markerId.Substring(2);
            //RouteValueDictionary parameters =
            //    new RouteValueDictionary  
            //        { 
            //            {"trackId", trackId }
            //        };
            //VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "tracks", parameters);
            //string script = "window.open('" + vpd.VirtualPath + "', '_blank')";
            
            string script = "window.open('" + Page.ResolveUrl("/tracks") + "/" + trackId + "', '_blank')";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "trackdetails", script, true);
            return script;
        }
    }
}